package com.ruoyi.myweb.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.myweb.domain.FoodShop;
import com.ruoyi.myweb.service.IFoodShopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.myweb.domain.FoodDish;
import com.ruoyi.myweb.service.IFoodDishService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜品管理Controller
 * 
 * @author ruoyi
 * @date 2023-09-19
 */
@RestController
@RequestMapping("/food/dish")
public class FoodDishController extends BaseController
{
    @Autowired
    private IFoodDishService foodDishService;

    @Autowired
    private IFoodShopService foodShopService;

    /**
     * 查询菜品管理列表
     */
    @PreAuthorize("@ss.hasPermi('food:dish:list')")
    @GetMapping("/list")
    public TableDataInfo list(FoodDish foodDish)
    {
        startPage();
        Long shopId;
        FoodShop foodShop = new FoodShop();
        if(!this.getUsername().equals("admin")){
            Long ownerId = this.getUserId();
            foodShop.setOwnerId(ownerId);
        }
        List<FoodShop> shoplist = foodShopService.selectFoodShopList(foodShop);
        if(shoplist != null && shoplist.size()>0){
            shopId = shoplist.get(0).getId();
            foodDish.setShopId(shopId);
        }


        List<FoodDish> list = foodDishService.selectFoodDishList(foodDish);
        return getDataTable(list);
    }

    /**
     * 导出菜品管理列表
     */
    @PreAuthorize("@ss.hasPermi('food:dish:export')")
    @Log(title = "菜品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodDish foodDish)
    {
        List<FoodDish> list = foodDishService.selectFoodDishList(foodDish);
        ExcelUtil<FoodDish> util = new ExcelUtil<FoodDish>(FoodDish.class);
        util.exportExcel(response, list, "菜品管理数据");
    }

    /**
     * 获取菜品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('food:dish:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(foodDishService.selectFoodDishById(id));
    }

    /**
     * 新增菜品管理
     */
    @PreAuthorize("@ss.hasPermi('food:dish:add')")
    @Log(title = "菜品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FoodDish foodDish)
    {
        return toAjax(foodDishService.insertFoodDish(foodDish));
    }

    /**
     * 修改菜品管理
     */
    @PreAuthorize("@ss.hasPermi('food:dish:edit')")
    @Log(title = "菜品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FoodDish foodDish)
    {
        return toAjax(foodDishService.updateFoodDish(foodDish));
    }

    /**
     * 删除菜品管理
     */
    @PreAuthorize("@ss.hasPermi('food:dish:remove')")
    @Log(title = "菜品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(foodDishService.deleteFoodDishByIds(ids));
    }
}
